package com.wnhz.work.yz.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.Childmember;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ---名下会员
 */
public class UnderMemberActivity extends BaseActivity implements View.OnClickListener {
    private TextView title;
    private RecyclerView my_recycler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_under_member);

        initView();
    }

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("名下会员");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        my_recycler = (RecyclerView) findViewById(R.id.my_recycler);
        my_recycler.setLayoutManager(new GridLayoutManager(this, 2, LinearLayoutManager.VERTICAL, false));

        upload();
    }

    private static final String TAG = "UnderMemberActivity";
    private List<Childmember.InfoEntity> infos = new ArrayList<>();

    private void upload() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        XUtil.Post(Url.UCENTER_CHILDMEMBER, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i(TAG, "onSuccess:UCENTER= " + result);
                infos.clear();
                Gson gson = new Gson();
                Childmember child = gson.fromJson(result, Childmember.class);
                infos = child.getInfo();
               // adapter.notifyDataSetChanged();
              //  Log.e("---", infos.size() + "");
            }

            @Override
            public void onFinished() {
                super.onFinished();
//                setData();
                Log.e("---", infos.size() + "");
                adapter = new Adapter(UnderMemberActivity.this, infos);
                my_recycler.setAdapter(adapter);

            }


            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }

    private void setData() {
        Log.e("---", infos.size() + "");


    }

    private Adapter adapter;

    class Adapter extends BaseRVAdapter {
        private List<Childmember.InfoEntity> infos = new ArrayList<>();

        /**
         * 构造器
         *
         * @param context 上下文
         * @param list    数据集合
         */
        public Adapter(Context context, @NonNull List list) {
            super(context, list);
            infos = list;
        }

        @Override
        public int getLayoutId(int viewType) {
            return R.layout.item_child_member;
        }

        @Override
        public void onBind(BaseViewHolder holder, int position) {
            x.image().bind(holder.getImageView(R.id.head), infos.get(position).getPerson_img());
            holder.setTextView(R.id.name, infos.get(position).getPerson_name());
            holder.setTextView(R.id.phone, infos.get(position).getTelephone());
            holder.setTextView(R.id.money, "" + infos.get(position).getTotal_money());

        }

//        @Override
//        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
//
//        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            default:
                break;
        }
    }
}
